/*
 * Copyright © 2017-2018 All Rights Reserved
 * 上海仰空网络科技有限公司 版权所有
 */

package org.chw.provider.controller;

import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;

import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @className ConfigController
 * @description
 */
@RestController
@RequestMapping("/config")
public class ConfigController {

  @Value("${useLocalCache:false}")
  private boolean useLocalCache;

  @NacosInjected
  private NamingService namingService;

  public void setUseLocalCache(boolean useLocalCache) {
    this.useLocalCache = useLocalCache;
  }

  @RequestMapping(value = "/get", method = RequestMethod.GET)
  @ResponseBody
  public boolean get() {
    return useLocalCache;
  }

  @RequestMapping(value = "/service", method = RequestMethod.GET)
  @ResponseBody
  public List<Instance> getServiceName(@RequestParam("name") String serviceNmae)
      throws NacosException {
    return this.namingService.getAllInstances(serviceNmae);
  }
}
